<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 发布主题
 *
 * @author      Eden
 * @datetime    2016/12/10 22:46
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Publish extends ADM_Controller
{
    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 主页面
     */
    public function index()
    {
        $this->load->view(parent::TMP_DIR . '/bbs/publish.html');
    }

    public function add()
    {

    }

    /**
     * 上传缩略图
     */
    public function uploadAttachment()
    {
        $send['state'] = false;
        do {

            $thumbWidth = 100;
            $thumbHeight = 100;

            if ($this->input->method(true) !== 'POST') {
                $send['error'] = '非法请求';
                break;
            }

            $width = $this->input->get('width');
            $height = $this->input->get('height');
            if (!empty($width) && !empty($height)) {
                $thumbWidth = intval($width);
                $thumbHeight = intval($height);
            }

            $res = local($_FILES['file']);
            if (empty($res)) {
                $send['error'] = '上传失败';
            } else {
                $send['state'] = true;
                $send['data'] = array_merge($res, array(
                    'thumb' => thumb($res['key'], $thumbWidth, $thumbHeight),
                    'source' => thumb($res['key']),
                    ));
            }
        } while(false);
        $this->output->send($send);
    }

    /**
     * 发布主题
     */
    public function submit()
    {
        $send['state'] = false;
        do {

            if ($this->input->method(true) !== 'POST') {
                $send['error'] = '非法请求';
                break;
            }

            $posts = $this->input->post();
            if (empty($posts['content'])) {
                $send['error'] = '主题内容不能为空';
                break;
            }

            $this->load->model('admin/bbs/Index_Model', 'bbs');
            if ($this->bbs->publish($posts)) {
                $send['state'] = true;
            } else {
                $send['error'] = '发布失败';
            }

        } while(false);

        $this->output->send($send);
    }

}
